package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Course;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.University;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IUniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/University")
public class UniversityController {
    @Autowired
    IUniversityService universityService;

    @RequestMapping(value = {"/toAddUniversity"})
    public String toAddUniversity() {
        return "toadduniversity";
    }

    @RequestMapping(value = {"/toUpdateUniversity"})
    public ModelAndView toUpdateUniversity(Integer id) {
        //int id = Integer.parseInt(model.getAttribute("id").toString());
        University university = universityService.queryUniversityById(id);
        ModelAndView model = new ModelAndView("toupdateuniversity");
        model.addObject("university", university);
        return model;
    }

    @RequestMapping(value = {"/insertUniversity"})
    @ResponseBody
    public String insertUniversity(University university) {
        universityService.insertUniversity(university);
        return "OK";
    }

    @RequestMapping("/queryAllUniversity")
    public String queryAllUniversity(Model model,@RequestParam(value = "pageNow",defaultValue = "1")int pageNow,
                                     @RequestParam(value = "pageSize",defaultValue = "5")int pageSize)
    {
        int totalCount = universityService.queryUniversityCount();
        int pageCount = (int)Math.ceil((double)totalCount/pageSize);
        List<University> universities = universityService.queryUniversityByPage(pageNow,pageSize);
        model.addAttribute("universities", universities);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "university";
    }

    @ResponseBody
    @GetMapping("queryUniversityById")
    public University queryUniversityById(Integer id)
    {
        return universityService.queryUniversityById(id);
    }

    @ResponseBody
    @RequestMapping("deleteUniversityById")
    public String deleteUniversityById(Integer id)
    {
        universityService.deleteUniversityById(id);
        return "OK";
    }

    @ResponseBody
    @RequestMapping("updateUniversity")
    public String updateUniversity(University university)
    {
        universityService.updateUniversityById(university);
        return "OK";
    }



}